﻿//------------------------------------------------------------------------------
// The contents of this file are subject to the Microsoft Public License (Ms-PL)
// You may obtain a copy of the License at http://psadmin.codeplex.com/license. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, 
// WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations 
// under the License.
// 
// The initial developer is Ben Foster (ben.foster@retroviz.com)
// Copyright (c) Retroviz Limited (http://www.retroviz.com).
// All Rights Reserved.
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using PSAdmin.Web.Infrastructure;
using PSAdmin.Core.Interfaces;
using StructureMap;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Context;

namespace PSAdmin.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class Global : System.Web.HttpApplication
    {
        private static ILoggerService _logger;

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "PagedRoute", 
                "{controller}/{action}/Page{page}",
                null,
                new { page = @"\d+" }
            );

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Script", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            
            RegisterRoutes(RouteTable.Routes);

            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();

            // having to open session here as startup task to creates roles needs a session 
            // before one is created in beginrequest event
            CurrentSessionContext.Bind(SessionFactory.OpenSession());
            
            Bootstrapper.Boot();

            _logger = ObjectFactory.GetInstance<ILoggerService>();

            ControllerBuilder.Current.SetControllerFactory(
                new StructureMapControllerFactory());

            _logger.Info("Application Started");
        }

        public static ISessionFactory SessionFactory = CreateSessionFactory();

        private static ISessionFactory CreateSessionFactory()
        {
            return new Configuration()
                        .Configure()
                        .SetProperty(NHibernate.Cfg.Environment.ConnectionStringName, System.Environment.MachineName)
                        .BuildSessionFactory();
        }

        protected Global()
        {
            BeginRequest += delegate
            {
                CurrentSessionContext.Bind(SessionFactory.OpenSession());
            };
            EndRequest += delegate
            {
                CurrentSessionContext.Unbind(SessionFactory).Dispose();
            };
        } 

    }
}